package com.qqtech.qquan.pub.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.agent.context.AdminPage;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.circle.enums.CircleBaseStatusDictEnum;
import com.qqtech.qquan.circle.model.CircleBaseQuery;
import com.qqtech.qquan.circle.model.CircleBaseVo;
import com.qqtech.qquan.circle.service.CircleBaseService;
import com.qqtech.qquan.mall.model.MallProductQuery;
import com.qqtech.qquan.mall.model.MallProductVo;
import com.qqtech.qquan.mall.service.MallProductService;
import com.qqtech.qquan.pub.enums.PromotionSourceTypeEnumDictEnum;
import com.qqtech.qquan.pub.enums.PromotionTargetTypeEnumDictEnum;
import com.qqtech.qquan.pub.enums.PubConst;
import com.qqtech.qquan.pub.model.PubPromotion;
import com.qqtech.qquan.pub.model.PubPromotionQuery;
import com.qqtech.qquan.pub.model.PubPromotionVo;
import com.qqtech.qquan.pub.service.PubPromotionService;
import com.qqtech.qquan.pub.service.PubSysParamService;
import com.qqtech.qquan.sys.controller.BaseAgentController;

@Controller
@RequestMapping(value = "/pub/promotion")
public class PubPromotionController extends BaseAgentController {
	@Resource
	private PubPromotionService pubPromotionService;
	@Resource
	private CircleBaseService circleBaseService;
	@Resource
	private PubSysParamService pubSysParamService;
	@Resource
	private MallProductService mallProductService;

	@Override
	protected BaseService<PubPromotion> getBaseService() {
		return pubPromotionService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/pubPromotion/pubPromotion-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(PubPromotionQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(),query.getSize(), new PageOrder(Direction.DESC, "id"));
		Page<PubPromotionVo> page = pubPromotionService.queryPageList(query, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}

	@RequestMapping(value = "/mallList", method = RequestMethod.GET)
	public String mallList() {
		return "/pubPromotion/pubPromotion-mallList";
	}

	@RequestMapping(value = "/mallList", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage mallList(MallProductQuery query, Integer promotion, HttpServletRequest request,
			HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		if (null!=promotion&&promotion != 0) {// 已推广的
			PubPromotionQuery promotionQuery = new PubPromotionQuery();
			promotionQuery.setSourceBizzType(PromotionSourceTypeEnumDictEnum.MALL_PRODUCT.getCode());
			List<PubPromotionVo> list = pubPromotionService.queryList(promotionQuery);
			if (null != list && list.size() > 0) {
				List<Integer> ids = new ArrayList<Integer>();
				for (PubPromotionVo pubPromotionVo : list) {
					ids.add(pubPromotionVo.getSourceBizzId());
				}
				if (promotion == 1) {
					query.setIncludeIds(ids);
				} else {
					query.setFilterIds(ids);
				}
			}
		}

		Pageable pageKit = new PageKit(query.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<MallProductVo> page = mallProductService.queryPageList(query, pageKit);
		boolean isPromotion = false;
		for (MallProductVo mallProductVo : page) {
			isPromotion = pubPromotionService.isSourcePromotion(mallProductVo.getId(),
					PromotionSourceTypeEnumDictEnum.MALL_PRODUCT.getCode());
			mallProductVo.setIsPromotion(isPromotion);
		}

		adminPage.setRows(page.getContent());
		adminPage.setTotal(page.getNumberOfElements());
		return adminPage;
	}

	@RequestMapping(value = "/toAdd")
	public String toAdd(Integer sourceBizzId, Integer sourceBizzType, String img, Model model) {
		CircleBaseQuery circleBaseQuery = new CircleBaseQuery();
		circleBaseQuery.setStatus(CircleBaseStatusDictEnum.NORMAL.getCode());
		List<CircleBaseVo> circles = circleBaseService.queryList(circleBaseQuery);
		String[] imgs = img.split(";");
		model.addAttribute("img", imgs[0]);
		model.addAttribute("circles", circles);
		model.addAttribute("sourceBizzId", sourceBizzId);
		model.addAttribute("sourceBizzType", sourceBizzType);
		return "/pubPromotion/pubPromotion-add";
	}

	@RequestMapping(value = "/toShow")
	public String toShow(Integer sourceBizzId, Integer sourceBizzType, String img, Model model) {
		PubPromotionQuery pubPromotionQuery = new PubPromotionQuery();
		pubPromotionQuery.setSourceBizzId(sourceBizzId);
		pubPromotionQuery.setSourceBizzType(sourceBizzType);
		List<PubPromotionVo> list = pubPromotionService.queryList(pubPromotionQuery);
		CircleBaseVo circleBaseVo = null;
		for (PubPromotionVo pubPromotionVo : list) {
			if (CircleBaseStatusDictEnum.NORMAL.getCode() == pubPromotionVo.getTargetBizzType()) {// 目标是圈子
				circleBaseVo = circleBaseService.getById(pubPromotionVo.getTargetBizzId());
				pubPromotionVo.setPromotionName(circleBaseVo.getName());
			}
		}
		model.addAttribute("list", list);
		String[] imgs = img.split(";");
		model.addAttribute("img", imgs[0]);
		model.addAttribute("sourceBizzId", sourceBizzId);
		return "/pubPromotion/pubPromotion-show";
	}

	/**
	 * 创建成功
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/create")
	@ResponseBody
	public Result create(PubPromotion create) {
		Result ret = new Result();
		create.setTargetBizzType(PromotionTargetTypeEnumDictEnum.CIRCLE.getCode());
		String days = pubSysParamService.getByKey(PubConst.PARAM_PUB_PROMOTION_AUTO_EXPIRE_DAYS).getValue();
		create.setAutoExpireAt(TimeUtil.addDays(TimeUtil.now(), Integer.valueOf(days)));
		pubPromotionService.insert(create);
		return ret;
	}

	/**
	 * 删除
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/del")
	@ResponseBody
	public Result del(Integer id) {
		Result ret = new Result();
		pubPromotionService.deleteById(id);
		return ret;
	}
}
